package com.loopy.fruns.web;

import com.google.gson.Gson;
import com.loopy.fruns.entity.Member;
import com.loopy.fruns.service.MemberService;
import com.loopy.fruns.service.impl.MemberServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;

public class MemberServlet extends BasicServlet {
    private MemberService memberService = new MemberServiceImpl();

//    @Override
//    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        doPost(request, response);
//    }

//    @Override
//    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
////        System.out.println("MemberServlet被调用");
//        String action = request.getParameter("action");
//        if("login".equals(action)){
//            login(request,response);
//        } else if ("register".equals(action)) {
//            register(request, response);
//        }else {
//            response.setContentType("text/html;charset=utf-8");
//            response.getWriter().write("请求参数有误");
//        }
//    }

    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userName = request.getParameter("user-name");
        String userPassword = request.getParameter("user-password");
        Member login = memberService.login(new Member(null, userName, userPassword, null));
        if (login == null) {
            request.setAttribute("username", userName);
            request.setAttribute("msg", "用户名或密码错误");
            request.getRequestDispatcher("/views/member/login.jsp").forward(request, response);
        } else {
            request.getSession().setAttribute("member", login);
            if("admin".equals(login.getUsername())){
                request.getRequestDispatcher("/views/manage/manage_menu.jsp").forward(request, response);
            }else {
                request.getRequestDispatcher("/views/member/login_ok.jsp").forward(request, response);
            }

        }
    }

    protected void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("user-name");
        String userpassword = request.getParameter("user-password");
        String useremail = request.getParameter("user-email");
        String code = request.getParameter("code");
        String token = (String) request.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        request.getSession().removeAttribute(KAPTCHA_SESSION_KEY);
        if (token != null && token.equalsIgnoreCase(code)) {
            if (!memberService.isExitsUserName(username)) {
//            System.out.println("用户名"+username+"不存在，可用");
                Member member = new Member(null, username, userpassword, useremail);
                if (memberService.registerMember(member)) {
//                System.out.println("注册成功");
                    request.getRequestDispatcher("/views/member/register_ok.jsp").forward(request, response);
                } else {
//                System.out.println("注册失败");
                    request.getRequestDispatcher("/views/member/register_fail.jsp").forward(request, response);
                }

            } else {
                request.getRequestDispatcher("/views/member/login.jsp").forward(request, response);
            }
        }else {
            request.setAttribute("msg","验证码不正确");
            request.setAttribute("username",username);
            request.setAttribute("email",useremail);
            request.setAttribute("active","register");
            request.getRequestDispatcher("/views/member/login.jsp").forward(request, response);
        }

    }


    protected void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().invalidate();
        resp.sendRedirect(req.getContextPath());
    }


    protected void isExitsUserName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String username = req.getParameter("user-name");
        boolean exitsUserName = memberService.isExitsUserName(username);
//        String resultJson="{\"isExit\":"+exitsUserName+"}";
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("isExits",exitsUserName);
        resultMap.put("email","agaaa.qq");
        //现在返回ajax请求数据是交给ajax处理的，不是交给浏览器的
        String resultJson = new Gson().toJson(resultMap);
        resp.getWriter().write(resultJson);
    }
}
